package com.situ.blog.dao.impl;

import com.situ.blog.dao.IBlogDao;
import com.situ.blog.pojo.Blog;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BlogDaoImpl2 implements IBlogDao {
    @Override
    public List<Blog> selectByPage(BlogQuery blogQuery) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Blog> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,name,image,click_count,content,type_id,status,is_deleted,create_time,update_time FROM blog WHERE 1=1";
            List<Object> queryList = new ArrayList<>();
            String queryName = blogQuery.getName();
            if (queryName != null && !"".equals(queryName)) {
                sql += " AND name LIKE ?";
                queryList.add("%" + queryName + "%");
            }
            sql += " LIMIT ?,?";
            int offset = (blogQuery.getPage() - 1) * blogQuery.getLimit();
            queryList.add(offset);
            queryList.add(blogQuery.getLimit());

            //预编译的Statement
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                preparedStatement.setObject(i + 1, queryList.get(i));
            }
            System.out.println(preparedStatement);
            resultSet  = preparedStatement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果没有返回false，如果有返回true，并且指向这一行
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String image = resultSet.getString("image");
                int clickCount = resultSet.getInt("click_count");
                String content = resultSet.getString("content");
                int typeId = resultSet.getInt("type_id");
                int status = resultSet.getInt("status");
                int deleted = resultSet.getInt("is_deleted");
                Date createTime = resultSet.getTimestamp("create_time");
                Date updateTime = resultSet.getTimestamp("update_time");
                Blog blog = new Blog(id, name, image, clickCount, content, typeId, status, deleted, createTime, updateTime);
                list.add(blog);
            }
            for (Blog blog : list) {
                System.out.println(blog);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }

        return list;
    }

    @Override
    public int selectTotalCount(BlogQuery blogQuery) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int totalCount = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT count(*) FROM blog WHERE 1=1";
            //构造一个集合存放所有的条件
            List<Object> queryList = new ArrayList<>();
            String queryName = blogQuery.getName();
            if (queryName != null && !"".equals(queryName)) {
                sql += " AND name LIKE ?";
                queryList.add("%" + queryName + "%");
            }
            //预编译的Statement
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                preparedStatement.setObject(i + 1, queryList.get(i));
            }
            System.out.println(preparedStatement);
            resultSet  = preparedStatement.executeQuery();
            while (resultSet.next()) {
                totalCount = resultSet.getInt(1);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }

        return totalCount;
    }

    @Override
    public void deleteById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "DELETE FROM blog WHERE id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            System.out.println(preparedStatement);
            int count = preparedStatement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, preparedStatement, null);
        }
    }

    @Override
    public void add(Blog blog) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "INSERT INTO blog(name,type_id,image,content) VALUES (?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, blog.getName());
            preparedStatement.setInt(2, blog.getTypeId());
            preparedStatement.setString(3, blog.getImage());
            preparedStatement.setString(4, blog.getContent());
            System.out.println(preparedStatement);
            int count = preparedStatement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, preparedStatement, null);
        }
    }

    @Override
    public Blog selectById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Blog blog = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "id,name,image,click_count,content,type_id,status,is_deleted,create_time,update_time WHERE id=?";
            //预编译的Statement
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            System.out.println(preparedStatement);
            resultSet  = preparedStatement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果没有返回false，如果有返回true，并且指向这一行
                String name = resultSet.getString("name");
                int credit = resultSet.getInt("credit");
                //blog = new Blog(id, name, credit);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }

        return blog;
    }

    @Override
    public void update(Blog blog) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "UPDATE blog SET name=?,credit=? WHERE id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, blog.getName());
            //preparedStatement.setInt(2, blog.getCredit());
            preparedStatement.setInt(3, blog.getId());
            System.out.println(preparedStatement);
            int count = preparedStatement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, preparedStatement, null);
        }
    }

    @Override
    public void updateStatus(String id, int status) {

    }

    @Override
    public List<Blog> selectAll() {
        return null;
    }

    @Override
    public void addExcel(Blog blog) {

    }
}
